﻿using System;
using System.Windows.Forms;

namespace WinFormRapor
{
    public partial class Form1 : Form
    {
        RaporClass rpr = new RaporClass();

        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
        }

        void Form1_Load(object sender, EventArgs e)
        {
            if (!rpr.InternetTest())
            {
                MessageBox.Show("Bağlantı Yok !\r\nİnternet bağlantınızı kontrol edip tekrar çalıştırın.");
                return;
            }
            else
            {
                rpr.CreateDirectoryOnFTP("ftp.mikroyardim.com", "mikroyardim.com-dev", "1Q2w3e4r", Config.Get.FirmaKod);
                rpr.RaporOku();
            }
        }

        private void ayarlarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Login frm = new Login();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Application.Restart();
            }
        }

        private void kapatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                foreach (var item in Config.Get.Jobs)
                {
                    item.KalanSure -= 1;

                    if (item.KalanSure <= 0)
                    {
                        if (item.RaporTipi == 0)
                        {
                            rpr.RaporOlustur(item.Sql, item.RaporAdi);
                        }
                        else
                        {
                            rpr.GrafikOlustur(item.Sql, item.RaporAdi, item.GrafikTipi);
                        }
                        rpr.RaporLisetele();
                        item.KalanSure = item.Sure;
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            try
            {
                timer1.Enabled = false;
                rpr.RaporOku();
            }
            catch (Exception)
            {

            }
            finally
            {
                timer1.Enabled = true;
            }
        }
    }
}
